import { Controller, Post, UseGuards, Body, Req } from '@nestjs/common';
import { AuthGuard } from '../guard/auth.guard';
import { CommonResult } from '../utils/result';
import { CommentService } from './comment.service';
import { UsersService } from '../user/user.service';
@Controller('/comment')
export class CommentController {
  constructor(
    private readonly commentService: CommentService,
    private readonly usersService: UsersService,
  ) {}
  @Post('/sendComment')
  @UseGuards(AuthGuard)
  async createCommentMessage(@Body() body: any, @Req() req: any) {
    const user = req.user;
    if (user.id === 0) {
      return CommonResult(null, false, 401, '请先登录后再评论');
    }

    if (body.content === '') {
      return CommonResult(null, false, 400, '评论内容不能为空');
    }
    const result = await this.commentService.createCommentContent(body, user);
    if (result) {
      this.usersService.setCountLevel(req.user.username, 80);
      return CommonResult(result, true, 200, '评论成功');
    }
  }

  // 查询某个评论
  @Post('/findComment')
  async findComment(@Body() body: any) {
    const result = await this.commentService.getCommentContent(body.noteId);
    if (result.length === 0) {
      return CommonResult([], false, 404, '没有找到该评论');
    }
    return CommonResult(result, true, 200, '查询成功');
  }
}
